var longFunc;
var callbackFunc;

function loadingModalCallback() {
	longFunc();
	$("#loadingModal").modal('hide');
	callbackFunc();
}

function showLoadingModal(func, callback) {
	var modal = $("#loadingModal");
	longFunc = func;
	callbackFunc = callback;
	modal.one('shown', loadingModalCallback);
	modal.modal('show');
}

function createDataTable(id, url, colDefs, searchEnabled, sortDir) {
	if (sortDir == undefined || sortDir == null || sortDir == "") {
		sortDir = "asc";
	}
	$('#' + id).dataTable({
		"bProcessing" : true,
		"bServerSide" : true,
		"sAjaxSource" : url,
		"aaSorting" : [ [ 0, sortDir ] ],
		"oLanguage" : {
			"sProcessing" : "Kérjük, várjon...",
			"sLengthMenu" : "_MENU_ találat megjelenítése",
			"sZeroRecords" : "Nincs megjeleníthető találat",
			"sInfo" : "_START_-tól _END_-ig megjelenítve. Összesen: _TOTAL_",
			"sInfoEmpty" : "0-tól 0-ig megjelenítve. Összesen: 0",
			"sInfoFiltered" : "(leszűrve _MAX_ találatból)",
			"sInfoPostFix" : "",
			"sSearch" : "Keresés",
			"sUrl" : "",
			"oPaginate" : {
				"sFirst" : "Első",
				"sPrevious" : "Előző",
				"sNext" : "Következő",
				"sLast" : "Utolsó"
			}
		},
		"fnServerData" : function(sSource, aoData, fnCallback) {
			/* Add some extra data to the sender */
			if (!(typeof serverDatas === 'undefined') && serverDatas != null) {
				for (var i = 0; i < serverDatas.length; i++) {
					var serverData = serverDatas[i];
					aoData.push({
						"name" : serverData,
						"value" : $("#" + serverData).val()
					});
				}
			}
			$.getJSON(sSource, aoData, function(json) {
				/*
				 * Do whatever additional processing you want on the callback,
				 * then tell DataTables
				 */
				fnCallback(json);
			});
		},
		"bFilter" : searchEnabled,
		"aoColumnDefs" : colDefs
	});
}

function getData(data,name){
	var names=name.split('.');
	var retval=data;
	for (var i=0; i<names.length; i++){
		retval=retval[names[i]];
	}
	return retval;
}

function colDef(index, name) {
	return {
		"sName" : name,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			return getData(data,name);
		}
	};
}

function colDefNotSortable(index, name) {
	return {
		"sName" : name,
		"bSortable" : false,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			return getData(data,name);
		}
	};
}

function linkFormattedColDef(index, baseLink, name) {
	return {
		"sName" : name,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			return "<a href=\"" + baseLink + "/" + getData(data,name) + "\">" + getData(data,name) + "</a>";
		}
	};
}

function booleanColDef(index, name) {
	return {
		"sName" : name,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			var bool = getData(data,name);
			if (bool) {
				return "<center>✔</center>";
			} else {
				return "<center>×</center>";
			}
		}
	};
}

function timestampColDef(index, name) {
	return {
		"sName" : name,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			var ts = getData(data,name);
			var date = new Date(ts);
			return "<center>" + date.toLocaleDateString() + " " + date.toLocaleTimeString() + "</center>";
		}
	};
}

function dateColDef(index, name) {
	return {
		"sName" : name,
		"aTargets" : [ index ],
		"mData" : function(data, type, full) {
			var ts = getData(data,name);
			var date = new Date(ts);
			return "<center>" + date.toLocaleDateString() + "</center>";
		}
	};
}

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}